
import { getApplicationContainer } from './../../get-application-container';
// index.ts
// 获取应用实例
const app = getApp<IAppOption>()

Page({
    data: {
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
    },
    // 事件处理函数
    bindViewTap() {
        wx.navigateTo({
            url: '../logs/logs',
        })
    },
    async onLoad() {
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                        success: async res => {
                            // 可以将 res 发送给后台解码出 unionId
                            this.updateUserInfo({
                                detail: res
                            });
                        },
                    })
                }
            },
        });
    },
    async updateUserInfo(e: any) {
        if (e && e.detail && e.detail.userInfo) {
            getApplicationContainer().dialogService.alert('请授权', async () => {
                await getApplicationContainer().authService.setToken(e.detail.signature);
                wx.switchTab({
                    url: '/pages/mine/index'
                })
            });
        }
    },
    register() {
        return new Promise((resolve, reject) => {
            wx.login({
                success: async (res) => {
                    
                    await getApplicationContainer().authService.login(res.code);
                    resolve(true);
                },
                fail: () => {
                    reject('获取失败')
                }
            });
        })

    },
    toHome: function(){
        wx.switchTab({url: '../home/index'});
    },
    getPhoneNumber(e: any) {
        console.log(e);
    }
})
